###############################################
data_regression<-data_student

data_regression<-data_regression %>%
  group_by(specializare_bac2,unitate_de_invatamant,specializare_lb,an) %>%
  mutate(one_class=ifelse(n()<=25,1,0)) %>%
  ungroup()


###############################################
#regressions
iv_track<-feols(grad_perc~entrance_perc+dec_town+n_hs_town_group+dec_town*n_students_town_yr+Unemployment_hs_bac*dec_town+Wages_hs_bac*dec_town+drop_hs_hs_bac*dec_town+I(class_mean_yr-class_mean)|
                       as.factor(an)+specializare_bac2+scoala_de_provenienta+town|
                       class_mean~dec_town*n_hs_town_group,
                     cluster=~town,
                     data=data_regression %>% filter(school_change==F & one_class==1))
summary(iv_track)

ols_track<-feols(grad_perc~entrance_perc+dec_town+n_hs_town_group+dec_town*n_students_town_yr+Unemployment_hs_bac*dec_town+Wages_hs_bac*dec_town+drop_hs_hs_bac*dec_town+I(class_mean_yr-class_mean)+class_mean|
                   as.factor(an)+specializare_bac2+scoala_de_provenienta+town,
                 cluster=~town,
                 data=data_regression %>% filter(school_change==F & one_class==1))
summary(ols_track)

###############################################
#export results
current_path<-rstudioapi::getActiveDocumentContext()$path
setwd(dirname(current_path))
fileConn<-file("07_table_05.txt")
writeLines(print(modelsummary(list(#"(1)"=consistancy,
  "(2)"=ols_track,
  "(2)"=iv_track),
  statistic = "std.error",
  estimate="{estimate}{stars}",
  stars=c('^{*}'=0.1,'^{**}'=0.05,'^{***}'=0.01),
  output="latex",
  #gof_map=metrics,
  gof_omit = 'AIC|R2 Pseudo|R2 Adj|R2 Within|BIC|Log.Lik.|Sigma',
  coef_omit='none',
  coef_rename=variables,
  escape=F)
), fileConn)
close(fileConn)


